package com.example.rwq_test.niuke.list;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2023/11/9
 */
public class ReverseCyc {
    private static ListNode reverseCyc1(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = head;
        ListNode next = null;
        int count = 0;
        while (cur != null && cur.next != null && count < 2) {
            next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
            count ++;
        }
        if (cur.next != null) {
            cur.next = reverseCyc1(cur.next);
        }
        return dummy.next;
    }

    private static ListNode reverseCyc2(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        int count = 0;
        while (cur != null && cur != null && count < 3) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
            count++;
        }
        if (cur != null) {
            head.next = reverseCyc2(cur);
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode dummy = new ListNode(-1);
        ListNode head = new ListNode(1);
        dummy.next = head;
        for (int i = 2; i < 12; i++) {
            ListNode listNode = new ListNode(i);
            head.next = listNode;
            head = head.next;
        }

//        ListNode listNode = reverseCyc1(dummy.next);
        ListNode listNode = reverseCyc2(dummy.next);
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }
}
